package com.example.zs.mytest;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatCallback;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.example.zs.mytest.adapter.MyAdapter;
import com.example.zs.mytest.view.MultiStateView;
import com.example.zs.mytest.view.MySwipeRefreshLayout;
import com.example.zs.mytest.view.XRecyclerView.XRecyclerView;

import java.util.ArrayList;

import static com.example.zs.mytest.R.id.recycleView;
import static com.example.zs.mytest.R.id.swipelayout;

/**
 * Created by zs
 * Date：2017年 06月 19日
 * Time：17:03
 * —————————————————————————————————————
 * About:
 * —————————————————————————————————————
 */

public class SecondActivity extends FragmentActivity implements AppCompatCallback {

    private MySwipeRefreshLayout swipeLayout;
    private MultiStateView multiStateView;
    private XRecyclerView recyclerView;
//    private RecyclerView recyclerView;
    private ArrayList<String> mData = new ArrayList<>();
    private MyAdapter mAdaper;

    private int mScrollY = 0;
    private View mHeaderView;
    private Toolbar mToolbar;

    private AppCompatDelegate delegate;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        delegate = AppCompatDelegate.create(this, this);
        delegate.setContentView(R.layout.second_layout);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        delegate.setSupportActionBar(mToolbar);
        mHeaderView = LayoutInflater.from(this).inflate(R.layout.my_header_layout, null);
        swipeLayout = (MySwipeRefreshLayout) findViewById(swipelayout);
        multiStateView = (MultiStateView) findViewById(R.id.multiStateView);
        recyclerView = (XRecyclerView) findViewById(recycleView);
        mAdaper = new MyAdapter(this);
        for (int i = 0; i < 10; i++) {
            mData.add("数据" + i);
        }
        swipeLayout.setRefreshing(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addHeaderView(mHeaderView);
//        recyclerView.setPullRefreshEnabled(false);
//        recyclerView.setRefreshing(true);
        recyclerView.setAdapter(mAdaper);
        mAdaper.bindData(mData);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    View firstVisibleItem = recyclerView.getChildAt(0);
                    int firstItemTop = layoutManager.getDecoratedTop(firstVisibleItem);
                    if (firstItemTop == 0) {
                        swipeLayout.setEnabled(true);
                    } else {
                        swipeLayout.setEnabled(false);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                mScrollY += dy;
                mScrollY = -mHeaderView.getTop();
//                if (mScrollY < mBackgroudHeight) {
//                    setTopLayoutAlpha((int) (mScrollY / (float) mBackgroudHeight * 255));
//                } else {
//                    setTopLayoutAlpha(255);
//                }
            }
        });
    }

    @Override
    public void onSupportActionModeStarted(ActionMode mode) {

    }

    @Override
    public void onSupportActionModeFinished(ActionMode mode) {

    }

    @Nullable
    @Override
    public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
        return null;
    }

}
